<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/10/27
  Time: 22:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>

</head>
<body>
<%!
    //增加Cookie
    public Cookie addCookie(String name,String value,int Day){
        int second=60*60*24*Day;
        Cookie cookie=new Cookie(name,value);
        cookie.setMaxAge(second);
        return cookie;

    }
    //删除Cookie
    public void  delCookie(String name){
        Cookie[] cookies = request.getCookies();
        if(null==cookies) {
            System.out.println("没有cookie==============");
        }else{
            for(Cookie cookie : cookies){
                if(cookie.getName().equals(name)){
                    cookie.setValue(null);
                    cookie.setMaxAge(0);// 立即销毁cookie
                    cookie.setPath("/");
                    System.out.println("被删除的cookie名字为:"+cookie.getName());
                    response.addCookie(cookie);
                    break;
                }
            }
        }
    }
    //修改
    public void editCookie(String name;String value){
        Cookie[] cookies = request.getCookies();
        if(null==cookies) {
            System.out.println("没有cookie==============");
        }else{
            for(Cookie cookie : cookies){
                if(cookie.getName().equals(name)){
                    System.out.println("原值为:"+cookie.getValue());
                    cookie.setValue(value);
                    cookie.setPath("/");
                    cookie.setMaxAge(30* 60);// 设置为30min
                    System.out.println("被修改的cookie名字为:"+cookie.getName()+",新值为:"+cookie.getValue());
                    response.addCookie(cookie);
                    break;
                }
            }
        }


    }
    //得到cookie的值
    public String  getCookieValue(String name){
        Cookie[] cookies = request.getCookies();
        if(null==cookies) {
           return null;
        }else{
            for(Cookie cookie : cookies){
                if(cookie.getName().equals(name)){
                    return cookie.getValue().toString();

                }
            }
        }
        return null;
    }
%>
</body>
</html>
